package org.example.utils;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.example.action.IndexAction;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class Router {
    public static final Router ME = new Router();
    private Map<String, Action> store;

    /**
     * 最简单的路由器 一个map
     */
    public Router() {
        try {
            store = new HashMap<>();
            store.put("/a1", new IndexAction());
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    /**
     * 通过url找到 hander
     * @param path
     * @param req
     * @param res
     * @throws NoSuchMethodException
     * @throws IllegalAccessException
     * @throws InstantiationException
     * @throws IOException
     */
    public void Do(String path,HttpServletRequest req, HttpServletResponse res) throws NoSuchMethodException, IllegalAccessException, InstantiationException, IOException {
        Action hander = store.get(path);
        hander.index(req,res);
    }

}
